package com.like.adapter;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.like.entity.EventWrapper;
import com.like.entity.M;
import com.like.entity.MeetingCourseListResp;
import com.like.hospitalworld.DocumentDetailActivity;
import com.like.hospitalworld.R;
import com.like.utils.ImageUtils;

import org.greenrobot.eventbus.EventBus;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.List;

public class CourseAdapter extends BaseAdapter {

    private List<MeetingCourseListResp.MeetingCourse> mCourses;
    private M m;

    public CourseAdapter(List<MeetingCourseListResp.MeetingCourse> courses, M m) {
        this.mCourses = courses;
        this.m = m;
    }

    public void update(List<MeetingCourseListResp.MeetingCourse> courses, boolean append) {
        if (append)
            this.mCourses.addAll(courses);
        else
            this.mCourses = courses;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mCourses.size();
    }

    @Override
    public MeetingCourseListResp.MeetingCourse getItem(int position) {
        return mCourses.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder vh;
        MeetingCourseListResp.MeetingCourse course = getItem(position);
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_courseware, parent, false);
            vh = new ViewHolder(convertView);
            convertView.setTag(vh);
        } else
            vh = (ViewHolder) convertView.getTag();
        vh.setTags(position);
        vh.sTitle.setText(course.title);
        vh.sAuthor.setText(course.author);
        ImageUtils.showImage(m.mContext, course.titlePage, m.getToken(), vh.sTitleImg);
        return convertView;
    }

    public class ViewHolder {
        @ViewInject(R.id.activity_iv)
        public ImageView sTitleImg;
        @ViewInject(R.id.title)
        public TextView sTitle;
        @ViewInject(R.id.author)
        public TextView sAuthor;
        @ViewInject(R.id.read_all)
        public Button sReadAll;

        public void setTags(int position) {
            sReadAll.setTag(position);
        }

        public ViewHolder(View convertView) {
            x.view().inject(this, convertView);
        }

        @Event(R.id.read_all)
        private void readAllClick(View v) {
            int position = (int) v.getTag();
            MeetingCourseListResp.MeetingCourse course = getItem(position);
            EventBus.getDefault().postSticky(new EventWrapper(course, DocumentDetailActivity.class, DocumentDetailActivity.REQUEST_COURSE_INFO));
            Intent intent = new Intent(m.mContext, DocumentDetailActivity.class);
            m.mContext.startActivity(intent);
        }

    }
}
